package rio.swarawan.directionjson;

import com.google.android.maps.*;

import rio.swarawan.directionjson.parser.*;
import android.app.*;
import android.graphics.Color;
import android.os.*;
import android.view.Menu;

public class DemoDirectionMapsusingJSONActivity extends MapActivity {

	MapView mapView;

	// solo : -7.56229,110.816517
	// jogja : -7.824269, 110.364532

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mapView = (MapView) findViewById(R.id.map_view);
		GeoPoint gpSolo = new GeoPoint((int) (-7.56229 * 1E6),
				(int) (110.816517 * 1E6));
		GeoPoint gpJogja = new GeoPoint((int) (-7.824269 * 1E6),
				(int) (110.364532 * 1E6));
		Route r = direction(gpSolo, gpJogja);
		RouteOverlay rOverlay = new RouteOverlay(r, Color.BLUE);
		mapView.getOverlays().add(rOverlay);
		mapView.getController().animateTo(gpSolo);
		mapView.getController().setZoom(15);
		mapView.setStreetView(true);
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	private Route direction(final GeoPoint geoStart, final GeoPoint geoFinish) {
		Parser parser;
		final StringBuffer stringBuff = new StringBuffer(
				"http://maps.googleapis.com/maps/api/directions/json?");
		stringBuff.append("origin=");
		stringBuff.append(geoStart.getLatitudeE6() / 1E6);
		stringBuff.append(',');
		stringBuff.append(geoStart.getLongitudeE6() / 1E6);
		stringBuff.append("&destination=");
		stringBuff.append(geoFinish.getLatitudeE6() / 1E6);
		stringBuff.append(',');
		stringBuff.append(geoFinish.getLongitudeE6() / 1E6);
		stringBuff.append("&sensor=true&mode=driving");
		parser = new GoogleParser(stringBuff.toString());
		Route r = parser.parse();
		return r;
	}
}